package com.sxygsj.android.personnelscreen.services.webserver.controller;

import android.text.TextUtils;
import com.google.gson.Gson;
import com.sxygsj.android.personnelscreen.main.MainActivity;
import com.tencent.mmkv.MMKV;
import com.yanzhenjie.andserver.annotation.Controller;
import com.yanzhenjie.andserver.annotation.GetMapping;
import com.yanzhenjie.andserver.annotation.PostMapping;
import com.yanzhenjie.andserver.annotation.RequestParam;
import com.yanzhenjie.andserver.annotation.ResponseBody;
import java.util.WeakHashMap;

@Controller
public class PersonnelScreenController {

    @GetMapping("/")
    public String openWeb() {
        return "forward:/personnel.html";
    }

    @ResponseBody
    @PostMapping("/setConfugure")
    public String setConfigureInfo(@RequestParam("masterServer") String masterServer,
                                @RequestParam("standbyServer") String standbyServer,
                                @RequestParam("cardReaderNumber") String cardReaderNumber,
                                @RequestParam("advertisingScreenTitle") String advertisingScreenTitle,
                                @RequestParam("pageIntervalTime") int pageIntervalTime,
                                @RequestParam("scrollPlaybackSpeed") float scrollPlaybackSpeed,
                                @RequestParam("distance") int distance){
        MMKV mmkv=MMKV.mmkvWithID("configureInfo");
       if (TextUtils.isEmpty(masterServer)){
           return "请输入主服务器地址";
       }

        if (TextUtils.isEmpty(standbyServer)){
            return "请输入备用服务器地址";
        }
        if (TextUtils.isEmpty(distance+"")){
            return "请输入检测距离";
        }
        if (TextUtils.isEmpty(cardReaderNumber)){
            return "请输入读卡器编号";
        }
        if (TextUtils.isEmpty(advertisingScreenTitle)){
            return "请输入广告屏标题";
        }

        if (TextUtils.isEmpty(pageIntervalTime+"")){
            return "请输入翻页间隔时间";
        }

        if (TextUtils.isEmpty(scrollPlaybackSpeed+"")){
            return "请输入滚动屏播放速度";
        }

        if (!masterServer.equals(MainActivity.Companion.getMasterServerIP())){
            MainActivity.Companion.setMasterServerIP(masterServer);
        }

        if (!standbyServer.equals(MainActivity.Companion.getStandbyServerIP())){
            MainActivity.Companion.setStandbyServerIP(standbyServer);
        }
        if (!advertisingScreenTitle.equals(MainActivity.Companion.getAdvertisingScreenTitle())){
            MainActivity.Companion.setMasterServerIP(advertisingScreenTitle);
        }


        if (scrollPlaybackSpeed!=MainActivity.Companion.getScrollPlaybackSpeed()){
            MainActivity.Companion.setScrollPlaybackSpeed(scrollPlaybackSpeed);
        }

        if (distance!=MainActivity.Companion.getDistance()){
            MainActivity.Companion.setDistance(distance);
        }
        if (cardReaderNumber.equals(MainActivity.Companion.getCardReaderNumber())){
            MainActivity.Companion.setCardReaderNumber(cardReaderNumber);
        }
        if (pageIntervalTime!=MainActivity.Companion.getPageIntervalTime()){
            MainActivity.Companion.setPageIntervalTime(pageIntervalTime);
        }

        mmkv.encode("masterServer",masterServer);
        mmkv.encode("standbyServer",standbyServer);
        mmkv.encode("distance",distance);
        mmkv.encode("cardReaderNumber",cardReaderNumber);
        mmkv.encode("advertisingScreenTitle",advertisingScreenTitle);
        mmkv.encode("pageIntervalTime",pageIntervalTime);
        mmkv.encode("scrollPlaybackSpeed",scrollPlaybackSpeed);

        return "保存成功";
    }


    @ResponseBody
    @PostMapping("/getConfugure")
    public String getConfigureInfo(){
        MMKV mmkv=MMKV.mmkvWithID("configureInfo");
        WeakHashMap<String,String> weakHashMap=new WeakHashMap<>();
        weakHashMap.put("masterServer",mmkv.decodeString("masterServer","172.16.128.101:8089"));
        weakHashMap.put("standbyServer",mmkv.decodeString("standbyServer","172.16.128.100:8089"));
        weakHashMap.put("distance",mmkv.decodeInt("distance",9)+"");
        weakHashMap.put("cardReaderNumber",mmkv.decodeString("cardReaderNumber","0005793C"));
        weakHashMap.put("advertisingScreenTitle",mmkv.decodeString("advertisingScreenTitle","集广煤业检卡装置"));
        weakHashMap.put("pageIntervalTime",mmkv.decodeInt("pageIntervalTime",10)+"");
        weakHashMap.put("scrollPlaybackSpeed",mmkv.decodeFloat("scrollPlaybackSpeed",3.0f)+"");
       return new Gson().toJson(weakHashMap);
    }
}
